package cn.com.enersun.iec61850.web.mapper;

import cn.com.enersun.iec61850.web.domain.DeviceList;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YYC on 2017/5/8.
 */
@Mapper
public interface DeviceListMapper {
    //指定类属性与数据库字段对应关系
    @Results({
            @Result(property = "devName", column = "devName"),
            @Result(property = "devIp", column = "devIp"),
            @Result(property = "devPort", column = "devPort"),
            @Result(property = "online", column = "online")
    })

    @Select("SELECT devName, devIp, devPort, online from devicelists WHERE devIp = #{devIp}")
    DeviceList selectDevice(String devIp);

    @Select("SELECT devName, devIp, devPort, online from devicelists")
    ArrayList<DeviceList> selectDevices();

    @Insert("INSERT into devicelists(id, devName, devIp, devPort, online) VALUES(#{id}, #{devName}, #{devIp}, #{devPort}, #{online})")
    //在执行INSERT语句之前给id属性自动生成UUID值
    @SelectKey(statement="select replace(uuid(),'-','')", keyProperty="id", resultType=String.class, before=true)
    int insertDevice(DeviceList deviceList);

    @Update("UPDATE devicelists SET devIp=#{devIp} WHERE devIp = #{devIp}")
    void updateDeviceIp(DeviceList deviceList);

    @Update("UPDATE devicelists SET devName=#{devName} WHERE devIp = #{devIp}")
    void updateDeviceName(DeviceList deviceList);

    @Update("UPDATE devicelists SET online=#{online} WHERE devIp = #{devIp}")
    void updateDeviceStatus(@Param("devIp")String devIp, @Param("online")boolean online);

    @Update("UPDATE devicelists SET devName=#{devName} online=#{online} WHERE devIp = #{devIp}")
    void updateDevice(DeviceList deviceList);

    @Delete("DELETE FROM devicelists WHERE devIp = #{devIp}")
    void deleteDevice(String devIp);
}
